<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="residentId">
              <JUserSelect ref="userSelect" v-model="model.residentId"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学校" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="schoolName">
              <a-input v-model="model.schoolName" placeholder="请输入学校"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学历" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="level">
              <j-dict-select-tag v-model="model.level" dictCode="education_level" placeholder="请选择学历" allowClear @change="onEducationLevelChange"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="几年制" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="year">
              <j-dict-select-tag v-model="model.year" dictCode="education_year" placeholder="请选择几年制" allowClear/>
<!--              <a-select v-model="model.year" :options="yearOptions" placeholder="请选择几年制" allowClear/>-->
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="年级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="grade">
<!--              <a-select v-model="model.grade" :options="gradeOptions" placeholder="请选择年级" allowClear/>-->
              <j-dict-select-tag v-model="model.grade" dictCode="education_grade" placeholder="请选择年级" allowClear/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="全日制" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fulltime">
<!--              <a-input-number v-model="model.fulltime" placeholder="请输入全日制" style="width: 100%" />-->
              <a-switch v-model="isFullTime" checked-children="是" un-checked-children="否"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.grade=='9'">
            <a-form-model-item label="毕业时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="graduateTime">
              <j-date placeholder="请选择毕业时间" v-model="model.graduateTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import JUserSelect from '@comp/jeecg/JUserSelect'

  export default {
    name: 'VcaEducationForm',
    components: {
      JUserSelect
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          fulltime:1,
          resident:{}
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          residentId: [
              { required: true, message: '请选择姓名!'},
           ],
           level: [
              { required: true, message: '请输入文化等级!'},
           ]
        },
        url: {
          add: "/villageCadreAssist/vcaEducation/add",
          edit: "/villageCadreAssist/vcaEducation/edit",
          queryById: "/villageCadreAssist/vcaEducation/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
      isFullTime: {
        set:function(newVal){
          this.$set(this.model, "fulltime", (newVal?1:0));
        },
        get:function (){
          return this.model.fulltime===1;
        }
      }
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        if(record["id"]){
          this.model["year"] = this.model["year"]+"";
          this.model["grade"] = this.model["grade"]+"";
          this.$nextTick(()=>{
            this.$refs.userSelect.data.push({
              value:this.model["residentId"],
              text:this.model["residentName"]
            });
            this.$refs.userSelect.seletedOption = this.model["residentId"];
          });
        }
        this.visible = true;
      },
      onEducationLevelChange (value,index){
        if(value==20){
          this.model.year = "6";
        }else if(value>=30 && value<50 || value==51 || value==60){
          this.model.year = "3";
        }else if(value==50){
          this.model.year = "4";
        }else if(value==52){
          this.model.year = "5";
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>